package com.hackordie.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hackordie.bindables.BindableLogin;
import com.hackordie.bindables.BindableSignin;

@Controller
@RequestMapping(value = "/auth")
public class AuthController extends AMainController{

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(ModelMap model, HttpServletRequest request) throws Exception {
		model.addAttribute(new BindableLogin());
		breadCrumbService.addNode("Login", 0, request);
		model.addAttribute("active_login", "active");
		return "auth/login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(ModelMap model, @Valid @ModelAttribute("bindableLogin") BindableLogin bindableLogin, BindingResult result)
			throws Exception {
		model.addAttribute("active_login", "active");
		return "auth/login";
	}

	@RequestMapping(value = "/signin", method = RequestMethod.GET)
	public String signin(ModelMap model, HttpServletRequest request) throws Exception {
		model.addAttribute(new BindableSignin());
		breadCrumbService.addNode("Signin", 0, request);
		model.addAttribute("active_signin", "active");
		return "auth/signin";
	}

	@RequestMapping(value = "/signin", method = RequestMethod.POST)
	public String signin(ModelMap model, @Valid @ModelAttribute("bindableSignin") BindableSignin bindableSignin,
			BindingResult result) throws Exception {
		model.addAttribute("active_signin", "active");
		if (result.hasErrors()) {
			return "auth/signin";
		} else {
			hodManager.createUser(bindableSignin.toUser());
			return "redirect:/auth/login";
		}
	}

	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(ModelMap model, HttpServletRequest request) throws Exception {
		breadCrumbService.addNode("Welcome", 0, request);
		model.addAttribute("active_welcome", "active");
		return "auth/welcome";
	}

	@RequestMapping(value = "/accessdenied", method = RequestMethod.GET)
	public String accessdenied() throws Exception {
		return "auth/accessdenied";
	}

	@RequestMapping(value = "/loginfailed", method = RequestMethod.GET)
	public String loginerror(ModelMap model, @Valid @ModelAttribute("bindableLogin") BindableLogin bindableAuth,
			BindingResult result) throws Exception {
		model.addAttribute("error", true);
		model.addAttribute("active_login", "active");
		return "auth/login";
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(ModelMap model, HttpServletRequest request)
			throws Exception {
		breadCrumbService.addNode("GoodBye", 0, request);
		return "auth/logout";
	}
}
